const express = require('express');

const common = require('../../libs/common');
const mysql = require('mysql');

var db = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'mysql123456',
    database: '180618'
});

module.exports = function(){
    var router = express.Router();

    //banners网页打开
    router.get('/',(req, res)=>{
        //get请求，修改、删除数据
        switch(req.query.act){
            case 'mod':
                console.log('mod');
                    db.query(`select * from banner_table WHERE id=${req.query.id}`,(err, data)=>{
                        if(err){
                            console.log(err);
                            res.status(500).send('database error').end();
                        }else{
                            db.query(`select * from banner_table`,(err, banners)=>{
                                if(err){
                                    res.status(500).send('database error').end();
                                }else{
                                    console.log(data[0]);
                                    res.render('admin/banners.ejs',{banners,mod_data:data[0]});
                                }
                            });  
                        }
                    });
                break;
            case 'del':
                console.log('del');
                db.query(`delete from banner_table where ID=${req.query.id}`,(err, data)=>{
                    if(err){
                        console.log(err);
                        res.status(500).send('database error').end();
                    }else{
                        console.log('重定向到banners');
                        res.redirect('/admin/banners');
                    }
                });
                break;
            default:
                db.query(`select * from banner_table`,(err, data)=>{
                    if(err){
                        res.status(500).send('database error').end();
                    }else{
                        var banners = data;
                        //var mod_data = null;
                        res.render('admin/banners.ejs',{banners});
                    }
                });
                break;
        }
    });

    //banners请求
    router.post('/',(req, res)=>{
        var title = req.body.title;
        var description = req.body.description;
        var href = req.body.href;

        if(!title || !description || !href){
            res.status(400).send('arg error').end();
        }else{
            if(req.body.mod_id){
                console.log('****:'+req.body.mod_id);
                db.query(`update banner_table set title='${req.body.title}',
                description='${req.body.description}',
                href='${req.body.href}' where id=${req.body.mod_id}`,(err, data)=>{
                    if(err){
                        res.status(500).send('database error').end;
                    }else{
                        res.redirect('/admin/banners');
                    }
                });
            }else{
                db.query(`insert into banner_table (title, description, href)
                value('${title}','${description}','${href}')`,(err, data)=>{
                    if(err){
                        res.status(500).send('database error').end;
                    }else{
                        res.redirect('/admin/banners');
                    }
                });
            }
        }
    });

    return router;
}